package com.stu.boke.controller.admin;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.stu.boke.entity.Comment;
import com.stu.boke.service.CommentService;

@Controller
@RequestMapping("/admin/comment")
public class CommentController {
	
	@Autowired
	private CommentService commentService;

	@RequestMapping("")
	public String indexComment(@RequestParam(required = false, defaultValue = "1") Integer pageIndex,
			@RequestParam(required = false, defaultValue = "10") Integer pageSize ,Model model) {
		PageInfo<Comment> commentPageInfo = commentService.listCommentByPage(pageIndex, pageSize);
        model.addAttribute("pageInfo", commentPageInfo);
        model.addAttribute("pageUrlPrefix","/admin/comment?pageIndex");
        return "Admin/Comment/index";
	}
	
	@RequestMapping(value = "/replySubmit")
	public String insertSubmitComment(Comment comment) {
		comment.setCommentCreateTime(new Date());
		commentService.insertSubmitComment(comment);
		return "redirect:/admin/comment";
	}
	
	@RequestMapping(value = "/edit/{id}")
	public ModelAndView editCommentView(@PathVariable("id") Integer id) {
		ModelAndView modelAndView = new ModelAndView();
		Comment comment = commentService.getCommentById(id);
		modelAndView.addObject("comment", comment);
		modelAndView.setViewName("Admin/Comment/edit");
		return modelAndView;
	}
	
	@RequestMapping(value = "/editSubmit")
	public String editCommentSubmit(Comment comment) {
		comment.setCommentCreateTime(new Date());
		commentService.updateComment(comment);
		return "redirect:/admin/comment";
	}
	
	@RequestMapping(value = "/reply/{id}")
	public ModelAndView replyArticle(@PathVariable("id") Integer id) {
		Comment comment = commentService.getCommentById(id);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("comment", comment);
        modelAndView.setViewName("Admin/Comment/reply");
        return modelAndView;
	}
	
	@RequestMapping(value = "/delete/{id}")
	public String deleteComment(@PathVariable("id") Integer id) {
		commentService.deleteById(id);
		return "redirect:/admin/comment";
	}
}
